package com.qf.student.web;

import com.alibaba.fastjson.JSON;
import com.qf.student.entity.Dept;
import com.qf.student.service.IDeptService;
import com.qf.student.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Dept")
public class DeptController extends HttpServlet {

    private IDeptService deptService = new DeptServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String action = req.getParameter("action");

        System.out.println("action:" + action);

        if ("add".equals(action)) {
            addDept(req, resp);
        } else if ("update".equals(action)) {
            updateDept(req, resp);
        } else if ("byId".equals(action)) {
            byId(req, resp);
        } else if ("del".equals(action)) {
            deleteDept(req, resp);
        } else if ("list".equals(action)) { // 这里的jsp
            getList(req, resp);
        } else if ("all".equals(action)) { // 所有部门的JSON数据
            all(req, resp);
        }
    }

    private void all(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 1.调用业务层获取数据
        List<Dept> list = deptService.list();

//        req.setAttribute("deptList", list);
        // 2.把集合转成JSON字符串响应给前端
        String jsonString = JSON.toJSONString(list);

        // 3.响应
        resp.getWriter().write(jsonString);
    }

    private void byId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("deptno");
        Dept dept = deptService.getById(Integer.parseInt(id));
        // 2.放到作用域
        req.setAttribute("dept", dept);

        // 3.跳转页面
        req.getRequestDispatcher("dept/updateDept.jsp").forward(req, resp);
    }

    private void getList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 1.获取数据
        List<Dept> list = deptService.list();

        // 2.放到作用域
        req.setAttribute("deptList", list);

        // 3.跳转页面
        req.getRequestDispatcher("dept/deptList.jsp").forward(req, resp);

    }

    private void deleteDept(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String deptno = req.getParameter("deptno");
        boolean remove = deptService.remove(Integer.parseInt(deptno));
        responceClient(remove, resp);
    }

    private void updateDept(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 1.获取表单参数
        String ename = req.getParameter("dname");
        String loc = req.getParameter("loc");
        String deptno = req.getParameter("deptno");

        // 2.封装
        Dept dept = new Dept(Integer.parseInt(deptno), ename, loc);

        // 3.插入，响应
        responceClient(deptService.update(dept), resp);
    }

    private void addDept(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 1.获取表单参数
        String ename = req.getParameter("dname");
        String loc = req.getParameter("loc");

        // 2.封装
        Dept dept = new Dept(null, ename, loc);

        // 3.插入
        boolean save = deptService.save(dept);

        // 4.响应
        responceClient(save, resp);
    }

    public void responceClient(boolean flag, HttpServletResponse resp) throws IOException {
        if (flag) {
            resp.sendRedirect("/Dept?action=list");
        } else {
            resp.getWriter().write("操作失败");
        }
    }
}
